// pages/submit/submit.js
Page({
  data: {
    taskList:['尚无任务'], //普通选择器数据
    index: 0, //普通选择器值索引
    openid:'', //志愿者的openid
    lostmanName:[],
    clue:'', //线索详情
  },
  /**
    * 任务下拉框
    */
  changeValue(e) {
    this.setData({
      index: e.detail.value
    })
    console.log("选择的任务index为：",this.data.index)
  },
  /**
   * 获取任务线索的输入
   */
  text_input:function(e){
    this.setData({
      clue:e.detail.value
    })
    console.log("获取到的任务线索输入为：",this.data.clue)
  },

  /**
   * 获取志愿者当前的任务列表
  */
  getActiveTaskList:function(){
    var token_get = wx.getStorageSync('token');//wx.getStorageSync(key)，获取本地缓存
    wx.request({
      url: 'https://www.lanotherl.com/volunteer/getActiveTaskList',
      data:{
        openid:this.data.openid
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': 'Bearer '+token_get
      },
      success:res=>{
        console.log("传进去的openid为：",this.data.openid);
        console.log("访问接口成功，返回的数据为：",res.data);
        this.setData({
          taskList:res.data.data.taskList
        })
        for(var i = 0; i < res.data.data.taskList.length; i++){
          this.setData({
            lostmanName:this.data.lostmanName.concat(res.data.data.taskList[i].lostmanName)
          })
          console.log(res.data.data.taskList[i].lostmanName)
        }
        console.log(this.data.lostmanName)
      },
    })
  },
  /**
   * 生命周期函数--监听页面显示
   * 1、获取家属的openid
   */
  onShow: function () { 
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        console.log('云函数获取到的openid: ', res.result.openId)
        var openid = res.result.openId;
        that.setData({
          openid: openid
        })
        this.getActiveTaskList();
      }
    })
  },

  /**
   * 保存提交信息
   */
  save:function(){
    var volunteerId = wx.getStorageSync('volunteerId');//wx.getStorageSync(key)，获取本地缓存
    console.log("志愿者编号为：",volunteerId);
    console.log("志愿者编号为：",this.data.taskList[this.data.index].taskId)
    wx.showModal({
      title: '线索提交',      
      content: '确定线索无误进行提交？',
      success:(res)=>{
        if(res.cancel){
          // 点击取消
        }else{
          // 点击确定，进行表单提交
          var data = {
            clue:this.data.clue,
            taskId:this.data.taskList[this.data.index].taskId,
            volunteerId:volunteerId
          }
          console.log("提交到服务器的表单数据为：",data)
          wx.request({
            url: 'https://www.lanotherl.com/task/clue',
            data,
            method:"POST",
            // Post请求
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            success:(res)=>{
              console.log("提交到服务器数据成功：",res)
            }
          })
        }
      }
    })
  },

})